import { VueComponent, Prop, Watch } from 'vue-typescript'
import * as VueRouter from 'vue-router';
import '../../img/a0.jpg'
//This is overkill but typescript is great! :)
Vue.use(VueRouter);
// Vue.use(VueResource);
var router = new VueRouter();
// var url = require("file-loader!./src/img/a0.png")
// var img=document.createElement('img');
// img.src=require('src/img/a0.jpg')

class Link {
    name:string;
    path:string;

    constructor(name:string, path:string){
        this.name = name;
        this.path = path;
    }
}



@VueComponent({
    template: require('./navbar.html'),
    style:require('./navbar.scss')
})
export class Navbar extends Vue {

    @Prop
    inverted:boolean = true; //default value
    terr:boolean=false;

    @Prop
    realName="";
    object:{default:string} = {default: 'Default object property!'}; //objects as default values don't need to be wrapped into functions

    links:Link[] = [
        new Link('Home', '/home'),
        new Link('About', '/about/se'),
        new Link('InquiryReleaseManage','/inquiry/inquiryReleaseManage'),    
        new Link('InquiryManage','/inquiry/inquiryManage'),
        new Link('InquiryAdd','/inquiry/inquiryAdd'),
        new Link('OrderReleaseManage','/order/orderReleaseManage'),
        new Link('OrderReleaseDetail','/order/orderReleaseDetail'),
        new Link('OrderReleaseAdd','/order/orderReleaseAdd'),
        new Link('OrderManage','/order/orderManage'),
        new Link('OrderManageDetail','/order/orderManageDetail'),
        
        new Link('UserInfo','/userInfo'),
        new Link('login','/login')
    ]

    @Watch('$route.path')
    pathChanged(){
         if(this.$route.path=="/"){
            router.go("/login")
        }
        this.isLogined=window.sessionStorage.getItem("logined")==="yes"?true:false;
        if(window.sessionStorage.getItem("logined")!="yes"&&this.$route.path!="/login"){
            router.go("/login")
        }
        console.log('Changed current path to: ' + this.$route.path);

        if(this.$route.path=="/home"){
            //登陆后获取用户名
            this.realName=JSON.parse(window.sessionStorage.getItem("userInfo")).realName;
        
            //清空背景图片
            $('body').css({'backgroundImage': '',});
        }

        if(this.$route.path=="/login"){
            this.isLogined=false;
            window.sessionStorage.removeItem("logined")//退出登陆后清除登陆信息
            window.sessionStorage.removeItem("userInfo");
            window.sessionStorage.removeItem("userName");
            

            // this.userName=""
            
        }
       
       
    }

    //该方法会在加载页面和刷新的时候触发
    ready(){
        $('#userImg').attr("src",'./img/a0.jpg');
        
        console.log(this.object.default);
        // console.log("path="+this.$route.path)
        // console.log("tiaozuianlioul")
        if(this.$route.path=="/"){//默认跳转登录页面
            router.go("/login");
        }else{
            if(this.$route.path=="/login"&&window.sessionStorage.getItem("logined")==="yes"){
                router.go("/home")
            }
            this.isLogined=window.sessionStorage.getItem("logined")==="yes"?true:false;
        }
        var userInfo = JSON.parse(window.sessionStorage.getItem("userInfo"));
         this.realName=userInfo?userInfo.realName:"";
     
    //    console.log("username="+this.userName)
    }
  
    toggle=function(data){
                // this.terr = !this.terr;
                $('#navbar'+data).toggleClass("active");
                $('#navbar'+data).siblings(".active").removeClass('active');
            }
    isLogined:boolean=false;

   
   

} 
